package org.cy.evolution.ioc.context;

public class EvolutionContext implements IContext {

    public static final int COMPONENT = 0;
    public static final int INJECTOR = 1;

    private static EvolutionContext instance = null;
    private IContext injectors = null;
    private IContext components = null;
    private int contextType;

    private EvolutionContext() {
    }

    public static EvolutionContext get(int contextType) {
        if (null == instance) {
            synchronized (EvolutionContext.class) {
                if (null == instance) {
                    instance = new EvolutionContext();
                }
            }
        }
        instance.init(contextType);
        return instance;
    }

    private void init(int contextType) {
        this.contextType = contextType;
        switch (contextType) {
            case COMPONENT: {
                if (null == components) {
                    components = new ComponentContext<>();
                }
            }
            break;
            case INJECTOR: {
                if (null == injectors) {
                    injectors = new InjectorContext<>();
                }
            }
            break;
            default:
                break;
        }
    }

    @Override
    public <T> T get(String className) {
        T ret = null;
        switch (contextType) {
            case COMPONENT:
                ret = components.get(className);
                break;
            case INJECTOR:
                ret = injectors.get(className);
                break;
        }
        return ret;
    }
}
